<template>
  <div class="app-container">
    <h1>讲师管理</h1>
    <p>讲师表单</p>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="讲师头像" prop="avatar">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader"
          action = "http://121.199.76.161:8083/admin/aliyun/oss/upload?module=avatar"
        >
          <img v-if="ruleForm.avatar" :src="ruleForm.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="讲师姓名" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="讲师资历" prop="career">
        <el-input v-model="ruleForm.career"/>
      </el-form-item>
      <el-form-item label="头衔" prop="level">
        <el-select v-model="ruleForm.level" placeholder="请选择头衔">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input-number v-model.number="ruleForm.sort" :min="0" label="排序值" />
      </el-form-item>
      <el-form-item label="讲师简介" prop="intro">
        <el-input v-model="ruleForm.intro" :autosize="{ minRows: 10, maxRows: 20}" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher.js'
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        career: '',
        level: '',
        avatar: '',
        sort: '',
        joinDate: '',
        isDeleted: '',
        intro: ''
      },
      fullscreenLoading: false,
      rules: {
        name: [
          { required: true, message: '请输入讲师名称', trigger: 'blur' }
        ],
        career: [
          { required: true, message: '请输入讲师资历,一句话说明讲师', trigger: 'change' }
        ],
        level: [
          { required: true, message: '请选择一个头衔', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请请输入排序值', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '请填写讲师简介', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请上传头像', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      teacherApi.getById(this.$route.params.id).then(res => {
        this.ruleForm = res.data.teacher
      })
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        this.fullscreenLoading = true
        if (valid) {
          if (this.ruleForm.id) {
            teacherApi.edit(this.ruleForm).then(res => {
              setTimeout(() => {
                this.fullscreenLoading = false
              }, 2000)
              console.log(res)
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.$router.push({ path: '/teacher' })
            })
          } else {
            teacherApi.save(this.ruleForm).then(res => {
              console.log(res)
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.$router.push({ path: '/teacher' })
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      console.log(URL.createObjectURL(file.raw))
      this.ruleForm.avatar = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      this.ruleForm.avatar = file
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
